//
//  RouteHistoryAnnotationView.m
//  leo
//
//  Created by 贾泽明 on 16/1/16.
//  Copyright © 2016年 mdx. All rights reserved.
//

#import "RouteHistoryAnnotationView.h"
#import "CustomCalloutView.h"
#import "SyncManager.h"

#define kBigWidth  32.f
#define kBigHeight 32.f

#define kSmallWidth  16.f
#define kSmallHeight 16.f

#define kBigHoriMargin 1.4f
#define kBigVertMargin 1.6f

#define kSmallHoriMargin 0.f
#define kSmallVertMargin 0.f

#define kPortraitWidth  25.f
#define kPortraitHeight 25.f

#define kCalloutWidth   240.0
#define kCalloutHeight  90.0

@interface RouteHistoryAnnotationView ()

@property (nonatomic, strong) UIImageView *mPortraitImageView;
@property (nonatomic, retain) NSString *mRadian;

@property (nonatomic, retain) NSString *mMainLabelValue;
@property (nonatomic, retain) NSString *mSubLabelValue;
@property (nonatomic, retain) NSString *mDateLabelValue;
@property (nonatomic, retain) NSString *mLocateTypeLabelValue;
@property (nonatomic, retain) NSString *mRadiusLabelValue;
@property (nonatomic, retain) NSString *mTypeLabelValue;

@end

@implementation RouteHistoryAnnotationView
@synthesize calloutView;
@synthesize mPortraitImageView     = _mPortraitImageView;
@synthesize mRadian                = _mRadian;

@synthesize mMainLabelValue        = _mMainLabelValue;
@synthesize mSubLabelValue         = _mSubLabelValue;
@synthesize mDateLabelValue        = _mDateLabelValue;
@synthesize mLocateTypeLabelValue  = _mLocateTypeLabelValue;
@synthesize mRadiusLabelValue      = _mRadiusLabelValue;
@synthesize mTypeLabelValue        = _mTypeLabelValue;

#pragma mark - Override
- (UIImage *)portrait
{
    return self.mPortraitImageView.image;
}

- (void)setPortrait:(UIImage *)portrait
{
    self.mPortraitImageView.image = portrait;
    
    CGFloat kBoundsWidth, kBoundsHeight;
    CGFloat kPicWidth = portrait.size.width, kPicHeight = portrait.size.height;
    
    if (portrait.size.width > portrait.size.height) {
        kBoundsWidth = kBoundsHeight = portrait.size.width;
    }
    else {
        kBoundsWidth = kBoundsHeight = portrait.size.height;
    }
    
    self.bounds = CGRectMake(0.f, 0.f, kBoundsWidth, kBoundsHeight);
    self.mPortraitImageView.bounds = CGRectMake((kBoundsWidth - kPicWidth) / 2, (kBoundsHeight - kPicHeight) / 2, kPicWidth, kPicHeight);
    self.center = CGPointMake(kBoundsWidth / 2, kBoundsHeight / 2);
    self.mPortraitImageView.center = self.center;
}

- (NSString *)radian
{
    return self.mRadian;
}

- (void)setRadian:(NSString *)radian
{
    self.mRadian = radian;
    self.mPortraitImageView.transform = CGAffineTransformMakeRotation([radian doubleValue]);
}

- (NSString *)mainLabelValue
{
    return self.mMainLabelValue;
}

- (void)setMainLabelValue:(NSString *)mainLabelValue
{
    self.mMainLabelValue = mainLabelValue;
}

- (NSString *)subLabelValue
{
    return self.mSubLabelValue;
}

- (void)setSubLabelValue:(NSString *)subLabelValue
{
    self.mSubLabelValue = subLabelValue;
}

- (NSString *)dateLabelValue
{
    return self.mDateLabelValue;
}

- (void)setDateLabelValue:(NSString *)dateLabelValue
{
    self.mDateLabelValue = dateLabelValue;
}

- (NSString *)locateTypeLabelValue
{
    return self.mLocateTypeLabelValue;
}

- (void)setLocateTypeLabelValue:(NSString *)locateTypeLabelValue
{
    self.mLocateTypeLabelValue = locateTypeLabelValue;
}

- (NSString *)radiusLabelValue
{
    return self.mRadiusLabelValue;
}

- (void)setRadiusLabelValue:(NSString *)radiusLabelValue
{
    self.mRadiusLabelValue = radiusLabelValue;
}

- (NSString *)typeLabelValue
{
    return self.mTypeLabelValue;
}

- (void)setTypeLabelValue:(NSString *)typeLabelValue
{
    self.mTypeLabelValue = typeLabelValue;
}

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            /* Construct custom callout. */
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x + 7.f,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
            
            UIFont *Midfont = [UIFont fontWithName:@"Arial" size:12.0f];
            UIFont *Smlfont = [UIFont fontWithName:@"Arial" size:10.0f];
            UILabel *mainLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 4, kCalloutWidth - 16, 20)];
            mainLabel.backgroundColor = [UIColor clearColor];
            mainLabel.textColor = [UIColor blackColor];
            mainLabel.font = Midfont;
            if([self isNullValue:self.mainLabelValue]){
                mainLabel.text = @"北京天安门";
            }else{
                mainLabel.text = self.mainLabelValue;
            }
            [self.calloutView addSubview:mainLabel];
            
            UILabel *subLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 24, kCalloutWidth - 16, 35)];
            subLabel.backgroundColor = [UIColor clearColor];
            subLabel.font = Midfont;
            subLabel.textColor = [UIColor colorWithRed:163.0/255 green:163.0/255 blue:163.0/255 alpha:1];
            subLabel.numberOfLines = 0;
            if ([self isNullValue:self.subLabelValue]) {
                if([self isNullValue:self.mainLabelValue])
                {
                    subLabel.text = @"北京天安门";
                }
                else
                {
                    subLabel.text = @"";
                }
            }else{
                subLabel.text = self.subLabelValue;
            }
            [self.calloutView addSubview:subLabel];
            
            UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 60, 65, 25)];
            dateLabel.backgroundColor = [UIColor clearColor];
            dateLabel.font = Smlfont;
            dateLabel.textColor = [UIColor blackColor];
            dateLabel.text = self.dateLabelValue;
            
            [self.calloutView addSubview:dateLabel];

            UILabel *locateTypeAndRadiusLabel = [[UILabel alloc] initWithFrame:CGRectMake(73, 60, 78, 25)];
            locateTypeAndRadiusLabel.backgroundColor = [UIColor clearColor];
            locateTypeAndRadiusLabel.font = Smlfont;
            locateTypeAndRadiusLabel.textColor = [UIColor blackColor];
            if([@"GPS定位" isEqualToString:self.locateTypeLabelValue]){
                locateTypeAndRadiusLabel.text  = @"GPS定位";
            }else{
                locateTypeAndRadiusLabel.text = [NSString stringWithFormat:@"%@, %@米", self.locateTypeLabelValue, self.radiusLabelValue];
            }
            [self.calloutView addSubview:locateTypeAndRadiusLabel];

            UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(151, 60, kCalloutWidth - 16 - 151, 25)];
            typeLabel.backgroundColor = [UIColor clearColor];
            typeLabel.font = Smlfont;
            typeLabel.textColor = [UIColor blackColor];
            typeLabel.text = self.typeLabelValue;

            [self.calloutView addSubview:typeLabel];
        }
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, kBigWidth, kBigHeight);
        self.backgroundColor = [UIColor clearColor];
        
        /* Create portrait image view and add to view hierarchy. */
        self.mPortraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kBigHoriMargin, kBigVertMargin, kPortraitWidth, kPortraitHeight)];
        [self addSubview:self.mPortraitImageView];
    }
    
    return self;
}

-(BOOL)isNullValue:(NSString *)value{
    if ([@"" isEqualToString:value] || [@"null" isEqualToString:value]
        || [@"(null)" isEqualToString:value] || value == NULL) {
        return YES;
    }
    return NO;
}

@end
